package org.example.socket;

import org.omg.PortableServer.THREAD_POLICY_ID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Random;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;

public class TestClient {

    private static final Logger LOGGER = LoggerFactory.getLogger(TestClient.class);

    private static final String HOST = "127.0.0.1";
    private static final int PORT = 8800;


    public void sendFile(String localFileName, String targetFileName) {
        DataOutputStream dos = null;
        FileInputStream fis = null;
        Socket socket = null;
        try {
            socket = new Socket(HOST, PORT);
            dos = new DataOutputStream(socket.getOutputStream());
            fis = new FileInputStream(localFileName);

            dos.writeUTF(targetFileName);
            dos.writeLong(fis.getChannel().size());

            byte[] buffer = new byte[4096];
            int bytesRead;

            while ((bytesRead = fis.read(buffer)) != -1) {
                dos.write(buffer, 0, bytesRead);
                dos.flush();
            }
            dos.flush();
            LOGGER.info("send file success,localFileName={},targetFileName={}", localFileName, targetFileName);
        } catch (Exception e) {
            LOGGER.error("send file error,localFileName={},targetFileName={}", localFileName, targetFileName, e);
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                LOGGER.error("close file input stream error", e);
                fis = null;
            }
            try {
                if (dos != null) {
                    dos.close();
                }
            } catch (IOException e) {
                LOGGER.error("close file input stream error", e);
                dos = null;
            }
            try {
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                LOGGER.error("close file input socket error", e);
                socket = null;
            }
        }


    }

    public static void main(String[] args) throws IOException {
        String localFileName = "D:\\soft\\ideaIU-2021.2.3.win.zip";
        String targetFileName = "C:/temp/ideaIU-2021.2.3.win.zip";
        TestClient testClient = new TestClient();
        for (int i = 0; i < 4; i++) {
            ForkJoinPool.commonPool().submit(
                    new Runnable() {
                        Random random = new Random();

                        @Override
                        public void run() {
                            testClient.sendFile(localFileName, "C:/temp/test-" + random.nextInt(1000) + "-ideaIU-2021.2.3.win.zip");
                        }
                    }
            );
        }

        try {
            ForkJoinPool.commonPool().shutdown();
            while (true) {
                boolean flag = ForkJoinPool.commonPool().awaitQuiescence(10, java.util.concurrent.TimeUnit.SECONDS);
                if (flag) {
                    TimeUnit.SECONDS.sleep(1);
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        LOGGER.info("send file success");
    }
}
